package com.jerry.template.module.mainpage.shouye.image.adapter;

import android.content.Context;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.jerry.template.R;
import com.jerry.template.base.BaseApplication;
import com.jerry.template.base.BaseViewHolder;
import com.jerry.template.base.MyBaseAdapter;
import com.jerry.template.module.mainpage.shouye.image.entity.ImageMessage;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by chenhuan on 2016/5/9.
 */
public class ImageAdapter extends MyBaseAdapter<ImageMessage.DataBean.DataBean1> {

    private List<ImageMessage.DataBean.DataBean1> data = new ArrayList<>();
    private Context context;
    private final ImageLoader imageLoader;

    public ImageAdapter(Context context, List<ImageMessage.DataBean.DataBean1> list) {
        super(context, list);
        this.data = list;
        this.context = context;
        imageLoader = ((BaseApplication) context.getApplicationContext()).getImageLoader();
    }

    @Override
    public int getLayoutId() {
        return R.layout.image_item;
    }

    @Override
    public BaseViewHolder getViewHolder() {
        return new ViewHodler();
    }

    private static final int TYPE_GIF = 1;
    private static final int TYPE_SINGLE_IMAGE = 2;
    private static final int TYPEE_MUTIPLE_IMAGE = 3;

    @Override
    public int getItemViewType(int position) {
        ImageMessage.DataBean.DataBean1.GroupBean group = getItem(position).getGroup();
        if (group != null) {
            if (group.getLarge_image() != null && group.getIs_gif() == 1) {
                return TYPE_GIF;
            } else if (group.getLarge_image_list() != null) {
                return TYPEE_MUTIPLE_IMAGE;
            } else if (group.getLarge_image() != null) {
                return TYPE_SINGLE_IMAGE;
            }
            //  包含large_image is_gif = 1 - gif 默认显示middle_image中的图片
            // large_image-> url_list[0] -> url

            // large_image_list - 多张图片 - 还包含thumb_image_list(小图片)
            // large_image_list集合 -> url

            // 包含large_image is_gif = 0
            // large_image-> url_list[0] -> url
        }
        return TYPE_GIF;
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public void bindViews(View view, BaseViewHolder viewHolder) {
        ViewHodler holder = (ViewHodler) viewHolder;
        try{
        if (data != null &&
                data.get(holder.position).getGroup().getUser() != null &&
                data.get(holder.position).getComments() != null &&
                data.get(holder.position).getGroup() != null &&
                data.get(holder.position) != null) {
            ImageMessage.DataBean.DataBean1.GroupBean group = data.get(holder.position).getGroup();
            ImageMessage.DataBean.DataBean1.GroupBean.UserBean user = data.get(holder.position).getGroup().getUser();

            String userHeadImageUrl = user.getAvatar_url();
            //加载用户头像，圆形
            //Glide.with(context).load(userHeadImageUrl).transform(new GlideCircleTransform(context)).into(holder.userHeadImage);
            imageLoader.displayImage(userHeadImageUrl, holder.userHeadImage);

            holder.userName.setText(user.getName());
            holder.content.setVisibility(View.GONE);

            if (group.getText() != null) {
                holder.content.setVisibility(View.VISIBLE);
                holder.content.setText(group.getText());
            }
            holder.tag.setText(group.getCategory_name());
            holder.upCount.setText(group.getDigg_count() + "");
            holder.downCount.setText(group.getBury_count() + "");
            holder.commentCount.setText(group.getComment_count() + "");
            holder.shareCount.setText(group.getShare_count() + "");

            int type = getItemViewType(holder.position);
            if (type == TYPE_GIF) {
                //
                holder.item_grid.setVisibility(View.GONE);
                String contentImageUrl = group.getLarge_image().getUrl_list().get(0).getUrl();
                //Glide.with(context).load(contentImageUrl).into(holder.image);
                imageLoader.displayImage(contentImageUrl, holder.image);
            } else if (type == TYPEE_MUTIPLE_IMAGE) {
                holder.item_grid.setVisibility(View.VISIBLE);
                List<ImageMessage.DataBean.DataBean1.GroupBean.LargeImageList> largeImageList = group.getLarge_image_list();
                ImageGridViewAdapter adapter = new ImageGridViewAdapter(largeImageList, context);
                holder.item_grid.setAdapter(adapter);
            }
            if (type == TYPE_SINGLE_IMAGE) {
                if (group.getLarge_image().getUrl_list() != null) {
                    String contentImageUrl = group.getLarge_image().getUrl_list().get(0).getUrl();
                    holder.item_grid.setVisibility(View.GONE);
                    //Glide.with(context).load(contentImageUrl).into(holder.image);
                    imageLoader.displayImage(contentImageUrl, holder.image);
                }

            }

           /* long groupId = group.getGroup_id();
            Intent intent = new Intent(context, ImageDetailActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("groupId",groupId);
            context.startActivity(intent);*/
        }}
        catch (Exception e){
            e.printStackTrace();
        }
    }

    public static class ViewHodler extends BaseViewHolder {
        @BindView(R.id.user_head)
        CircleImageView userHeadImage;
        @BindView(R.id.user_name)
        TextView userName;
        @BindView(R.id.content)
        TextView content;
        @BindView(R.id.duanzi_Tag)
        TextView tag;
        @BindView(R.id.image)
        ImageView image;
        @BindView(R.id.favourite_count)
        TextView upCount;
        @BindView(R.id.go_detail_count)
        TextView downCount;
        @BindView(R.id.comment_count)
        TextView commentCount;

        @BindView(R.id.item_grid)
        GridView item_grid;

        @BindView(R.id.share_count)
        TextView shareCount;

    }
}
